package org.activision.model.player;

import java.io.Serializable;

import org.activision.model.Container;
import org.activision.model.Item;
import org.activision.util.rs2cache.ItemDefinitions;

public class Banking implements Serializable {

	private static final long serialVersionUID = -9176611950582062556L;
	
	private Player player;
	
	public Container<Item> bank = new Container<Item>(468, false);
	public boolean withdrawNote = false;
	
	public void setPlayer(Player player) {
		this.player = player;
		for(Item item : bank.getItems()) {
			if(item != null)
				item.setDefinition(ItemDefinitions.forID(item.getId()));
		}
	}

	public void openBank() {
		player.getFrames().sendConfig2(563, 4194304);
		player.getFrames().sendConfig(1248, -2013265920);
		player.getFrames().sendInterface(762);
		player.getFrames().sendInventoryInterface(763);
		player.getFrames().sendAMask(0, 516, 762, 93, 40, 1278);
		player.getFrames().sendAMask(0, 27, 763, 0, 36, 1150);
		player.getFrames().sendBlankClientScript(1451);
		player.getFrames().sendItems(95, bank, false);
		player.getFrames().sendItems(31, player.getInventory().getContainer(), false);
	}
	
	public boolean addItemFromInventory(int slot, int itemID, int amount) {
		Item itemInInventory = player.getInventory().getContainer().get(slot);
		if(itemInInventory.getId()!=itemID)
			return false;
		int maxAmount = player.getInventory().numberOf(itemID);
		if(amount>maxAmount)
			amount = maxAmount;
		boolean isNoted = itemInInventory.getDefinition().isNoted();
		for(int i=0;i<bank.getSize();i++)
			if(bank.get(i)==null) {
				bank.set(i, new Item(isNoted?itemID-1:itemID, amount));
				break;
			} else if(bank.get(i).getId()==(isNoted?itemID-1:itemID)) {
				bank.set(i, new Item(isNoted?itemID-1:itemID, bank.get(i).getAmount()+amount));
				break;
			}
		player.getInventory().deleteItem(itemID, amount);
		refresh();
		return true;
	}
	
	public boolean deleteItemToInventory(int slot, int itemID, int amount, boolean butOne) {
		if(bank.get(slot).getId()!=itemID)
			return false;
		int maxAmount = bank.getNumberOff(itemID);
		if(maxAmount==0)//logic error
			return false;
		if(amount>maxAmount) {
			if(butOne == false) {
				amount = maxAmount;
			} else {
				amount = maxAmount-1;
			}
		}
		boolean shouldNote = (this.withdrawNote&&ItemDefinitions.forID(itemID+1).isNoted());
		boolean stackable = ItemDefinitions.forID(shouldNote?itemID+1:itemID).isStackable();
		if((player.getInventory().getFreeSlots()==0&&!stackable)||(player.getInventory().getFreeSlots()==0&&stackable&&!player.getInventory().contains(shouldNote?itemID+1:itemID)))
			return false;
		if((!stackable||!player.getInventory().contains(itemID))&&(amount>player.getInventory().getFreeSlots())&&withdrawNote==false)
				amount = player.getInventory().getFreeSlots();
		bank.remove(new Item(itemID, amount));
		player.getInventory().addItem(shouldNote?itemID+1:itemID, amount);
		bank.shift();
		refresh();
		return true;
	}
	
	public void refresh() {
		player.getFrames().sendItems(95, bank, false);
	}
}

